package com.zzz.code_replace;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;

public class TextParser {
	public static final String nl = "\r\n";

	public static void copy(String oldPath, String newPath, Map<String, String> map) {
		File oldFile = new File(oldPath);
		List<String> readUtf8Lines = FileUtil.readUtf8Lines(oldFile);
		String readUtf8String = FileUtil.readUtf8String(oldFile);
		String newLine = newLineExtract(readUtf8String);
		String lastLine = lastLineExtract(readUtf8String);
		String sumText = sumText(readUtf8Lines, newLine, lastLine, map);
		FileUtil.writeUtf8String(sumText, newPath);
	}

	public static String repaceText(String str, Map<String, String> map) {
		if (str == null) {
			return "";
		}
		if ("".equals(str)) {
			return "";
		}
		Set<Entry<String, String>> entrySet = map.entrySet();
		for (Entry<String, String> entry : entrySet) {
			String key = entry.getKey();
			String value = entry.getValue();
			str = str.replace(key, value);
		}
		return str;
	}
	
	public static String repaceText2(String str, Map<String, String> map) {
		if (str == null) {
			return "";
		}
		if ("".equals(str)) {
			return "";
		}
		Set<Entry<String, String>> entrySet = map.entrySet();
		for (Entry<String, String> entry : entrySet) {
			String key = entry.getKey();
			String value = entry.getValue();
			String upperFirstKey = StrUtil.upperFirst(key);
			String lowerFirstKey = StrUtil.lowerFirst(key);
			String underlineCaseKey = StrUtil.toUnderlineCase(lowerFirstKey);
			String upperFirstValue = StrUtil.upperFirst(value);
			String lowerFirstValue = StrUtil.lowerFirst(value);
			String underlineCaseValue = StrUtil.toUnderlineCase(lowerFirstValue);
			str = str.replace(underlineCaseKey, underlineCaseValue);
			str = str.replace(upperFirstKey, upperFirstValue);
			str = str.replace(lowerFirstKey, lowerFirstValue);
		}
		return str;
	}

	private static String sumText(List<String> readUtf8Lines, String newLine, String lastLine, Map<String, String> map) {
		if (readUtf8Lines == null) {
			return "";
		}
		if (readUtf8Lines.size() == 0) {
			return "";
		}
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < readUtf8Lines.size(); i++) {
			String line = readUtf8Lines.get(i);
			line = repaceText(line, map);
			sb.append(line);
			if (i != readUtf8Lines.size() - 1) {
				sb.append(newLine);
			} else {
				sb.append(lastLine);
			}
		}
		return sb.toString();
	}

	private static String newLineExtract(String content) {
		if (content == null || content.length() < 2) {
			return nl;
		}
		char[] chs = content.toCharArray();
		int index = -1;
		for (int i = 0; i < chs.length; i++) {
			Character ch = chs[i];
			if (ch == '\r' || ch == '\n') {
				index = i;
				break;
			}
		}
		if (index == -1) {
			return nl;
		}
		if (index + 1 >= content.length()) {
			return content.substring(content.length() - 1);
		} else {
			if (chs[index] == '\r') {
				if (chs[index + 1] == '\n') {
					return "\r\n";
				} else {
					return "\r";
				}
			} else if (chs[index] == '\n') {
				return "\n";
			} else {
				return nl;
			}
		}
	}

	private static String lastLineExtract(String content) {
		if (content == null || content.length() < 2) {
			return nl;
		}
		String lastStr = content.substring(content.length() - 2);
		if ("\r\n".equals(lastStr)) {
			return "\r\n";
		} else if ("\n".equals(lastStr.substring(lastStr.length() - 1))) {
			return "\n";
		} else if ("\r".equals(lastStr.substring(lastStr.length() - 1))) {
			return "\r";
		} else {
			return "";
		}
	}

}
